package com.szh.a0522_shenzhihao;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.szh.a0522_shenzhihao.Adapter.MyFragmentPagerAdapter;
import com.szh.a0522_shenzhihao.DBHelper.MyOpenHelper;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    TextView topTv;
    ViewPager fragmentVp;
    RadioGroup mainRg;
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;

    private RadioButton rbBook;
    private RadioButton rbAbout;
    private RadioButton rbMe;
    private MyFragmentPagerAdapter adapter;
    private MyOpenHelper myOpenHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        initView();//查找控件
        myOpenHelper = new MyOpenHelper(this);
        db=myOpenHelper.getWritableDatabase();
    }

    private void initView() {
        topTv = findViewById(R.id.top_tv);
        fragmentVp = findViewById(R.id.main_vp);
        mainRg = findViewById(R.id.main_rg);
        rbBook = (RadioButton) findViewById(R.id.rb_book);
        rbAbout = (RadioButton) findViewById(R.id.rb_about);
        rbMe = (RadioButton) findViewById(R.id.rb_me);
        mainRg.setOnCheckedChangeListener(this);
        fragmentVp.addOnPageChangeListener(this);
        fragmentVp.setCurrentItem(0);
        fragmentVp.setAdapter(adapter);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_book:
                fragmentVp.setCurrentItem(PAGE_ONE);
                break;
            case R.id.rb_about:
                fragmentVp.setCurrentItem(PAGE_TWO);
                break;
            case R.id.rb_me:
                fragmentVp.setCurrentItem(PAGE_THREE);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == 2) {
            switch (fragmentVp.getCurrentItem()) {
                case PAGE_ONE:
                    rbBook.setChecked(true);
                    break;
                case PAGE_TWO:
                    rbAbout.setChecked(true);
                    break;
                case PAGE_THREE:
                    rbMe.setChecked(true);
                    break;
            }
        }
    }
}
